package handler

import (
	"gowork/memberGrpc/fjmMessage"
	"gowork/memberGrpc/fjmServer/service"
	"context"
	"errors"
	"gowork/memberGrpc/model"
	"fmt"
	"encoding/json"
)

//会员套餐信息服务管理实现
type MemberHandlerCuda struct {
	Mip *service.MemberSriImp
	Tag string
}

//新增会员套餐信息的服务接口实现
func (mh *MemberHandlerCuda) InsertAll(ctx context.Context, request *fjmMessage.MemberListResponseOrRequest) (*fjmMessage.State, error) {
	var t = service.MemberSriImp{}
	mh.Mip = &t

	var mmm []*model.Member

	var response = &fjmMessage.State{}
	if uerr := json.Unmarshal([]byte(request.MemberListStr), &mmm); uerr == nil{
		receive := mh.Mip.InesrtSri(mmm)
		if receive["code"] == "200"{
			response.Message = receive["message"]
			response.Code = receive["code"]
			return response, nil
		}else{
			return nil, errors.New("插入失败")
		}
	}else{
		response.Message = "解析失败"
		response.Code = "400"
		return response, nil
	}
}

//查询会员套餐信息的服务接口实现
func (mh *MemberHandlerCuda) QueryAllInfo(ctx context.Context, request *fjmMessage.Empty) (*fjmMessage.MemberListResponseOrRequest, error) {
	var t = service.MemberSriImp{}
	mh.Mip = &t
	receive := mh.Mip.QuerySri()
	fmt.Println(receive)

	var response = &fjmMessage.MemberListResponseOrRequest{}
	if result, err := json.Marshal(receive); err == nil{
		fmt.Println(result)
		response.MemberListStr = string(result)
		return response, nil
	}else{
		return nil, errors.New("更新失败")
	}
}
